// pages/aritcle_detail/index.js
var common = require('../../lib/common.js');
var url = require('../../config/url.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: -1,
    aritcle: {},
    comment: {
      count: 0,
      data: []
    },

    // 浮动按钮
    menuBtn: [
      {
        type: "trash",
        title: "删除",
        span: 8
      },
      {
        type: "refresh",
        span: 8,
        title: "更新"
      },
      {
        type: "editor",
        span: 8,
        title: "编辑"
      }
    ],
    // 分页
    page: 1,
    isFrist: true,
  },
  onPullDownRefresh() {
    this.getAritcleDetail();
    this.getCommentList();
    wx.stopPullDownRefresh();
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      id: options.id,
    });
    this.getAritcleDetail();
    this.getCommentList();
  },

  onShow(options) {
    if (this.data.isFrist) return;
    wx.startPullDownRefresh();
  },

  // 选择浮动按钮
  onMenuClicked(e) {
    let index = e.detail.index;
    let article_id = this.data.id;
    switch (index) {
      case 0:
        // 删除
        wx.showModal({
          title: '提示',
          content: '确认删除这边文章吗，一旦删除将无法恢复？',
          success(res) {
            if (res.confirm) {
              wx.showLoading({ mask: true });
              common.post(url.deleteArticle, { article_id: article_id }, () => {
                wx.hideLoading();
                wx.showToast({
                  title: '删除成功',
                  icon: 'success',
                  duration: 2000
                });
                setTimeout(() => {
                  wx.navigateBack({
                    delta: 1
                  });
                }, 2000);

              })
            } else if (res.cancel) {
            }
          }
        })
        break;
      case 1:
        // 更新
        wx.showLoading({ mask: true });
        common.post(url.updateDateArticle, { article_id: article_id }, () => {
          wx.hideLoading();
          wx.showToast({
            title: '更新成功',
            icon: 'success',
            duration: 2000
          })
        })
        break;
      case 2:
        // 编辑
        if (this.data.aritcle.resource_type == 1) {
          wx.navigateTo({
            url: '/pages/aritcle_update_video/index?id=' + this.data.aritcle.id
          })
        } else {
          wx.navigateTo({
            url: '/pages/aritcle_update/index?id=' + this.data.aritcle.id
          })
        }
        break;
    }
  },

  // 获取文章详情
  getAritcleDetail() {
    common.post(url.getArticleDetail, { id: this.data.id }, (data) => {
      if (data.images == null)
        data.images = [];
      else {
        data.images = (data.images + '').split(',');
        data.images = data.images.map((n) => {
          return n;
        })
      }
      this.setData({ aritcle: data, isFrist: false });
    });
  },

  // 获取评论
  getCommentList() {
    let page = this.data.page;
    if (page === false) return;
    this.setData({
      page: page + 1
    })
    common.post(url.getArticleCommentList, { article_id: this.data.id, page: page }, (data) => {
      let comment = this.data.comment;
      comment.data = comment.data.concat(data.data);
      comment.count = data.count;
      this.setData({ comment });
      if (data.data.length == 0) {
        this.setData({
          page: false
        })
      }
    });
  },

  onShareAppMessage(e) {

  }

})